<?php defined('SYSPATH') or die('No direct script access.');

class Model_Region extends ORM
{   
    protected $_reload_on_wakeup = false;
    protected static $_current = null;
    
    public function get_region_by_url($url)
    {
        $region = ORM::factory('region')
                    ->where('url', '=', $url)
                    ->find();
        if (!$region->loaded()) {
            throw new HTTP_Exception_404('Такого региона нет');      
        }
        return $region;
    }
    
    public function set_as_current()
    {
        self::$_current = $this; 
    } 
    
    public function get_current()
    {
        return self::$_current;
    }
    
    public function get_rand_regions($limit = 8)
    {
        $max = 305;
        
        $ids = array();
        for ($i = 0; $i < $limit; $i++) {
            $id = mt_rand(1, $max);
            if (isset($ids[$id])) {
                $i--;    
            } else {
                $ids[$id] = $id;   
            }   
        }
        
		$regions = array();
		if (count($ids)) {
			$regions = ORM::factory('region')
						->where('id', 'IN', $ids)
						->find_all();
		}
                    
        return $regions;
    }
}